// TypeScript file

class CumtomRotation extends eui.Component {
    private moneyHighSubBtn: eui.Button;
    private moneyHighAddBtn: eui.Button;
    private moneyLowSubBtn: eui.Button;
    private moneyLowAddBtn: eui.Button;
    private timeSubBtn: eui.Button;
    private timeAddBtn: eui.Button;
    private closeBtn: eui.Button;

    private moneyHighContainer: eui.Component;
    private moneyLowContainer: eui.Component;
    private rotationTimeContainer: eui.Component;

    private highNum: NumPanel2;
    private lowNum: NumPanel2;
    private timeNum: NumPanel2;

    public context: MainPanel
    private bg: Bg = null;
    private moreUI: MoreUI;

    private rotationBtn: eui.Component;

    private rotationArr = {};

    private autoRotation: AutoRotationSetting;

    private timer: egret.Timer

    private tapName: string = null;
    private timeoutindex:number = 0;




    public constructor(moreui: MoreUI, autoRotation: AutoRotationSetting) {
        super();
        this.skinName = "cumtomRotation";
        this.moreUI = moreui;
        this.autoRotation = autoRotation;
        this.once(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
        this.timer = new egret.Timer(80, 0);
        this.timer.addEventListener(egret.TimerEvent.TIMER, this.timerFunc, this);
        // this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoveFromStage, this);
    }

    private onAddToStage(event: egret.Event) {
        if (this.bg != null) this.removeChild(this.bg.icon);
        this.bg = new Bg(this, 'mask_in_png');
        this.bg.setIcon(this.width, this.height, 0);

        this.highNum = new NumPanel2(0, 0, "");
        this.moneyHighContainer.addChild(this.highNum);
        this.highNum.setNum(Number(this.moreUI.context.data.Value.TokenInfo.Balance) + (this.autoRotation.getThisMoney() * 10));
        this.highNum.setScale(1);
        this.highNum.x = this.moneyHighContainer.width / 2;
        this.highNum.y = this.moneyHighContainer.height / 2;

        this.lowNum = new NumPanel2(0, 0, "");
        this.moneyLowContainer.addChild(this.lowNum)
        this.lowNum.setNum(Number(this.moreUI.context.data.Value.TokenInfo.Balance) - (this.autoRotation.getThisMoney() * 10));
        this.lowNum.setScale(1);
        this.lowNum.x = this.moneyLowContainer.width / 2;
        this.lowNum.y = this.moneyLowContainer.height / 2;

        this.timeNum = new NumPanel2(0, 0, "");
        this.rotationTimeContainer.addChild(this.timeNum);
        this.timeNum.setNum(1);
        this.timeNum.setScale(1);
        this.timeNum.x = this.rotationTimeContainer.width / 2;
        this.timeNum.y = this.rotationTimeContainer.height / 2;

        this.timeSubBtn.enabled = false


        this.initEvent();
    }

    public setNumber(): void {
        this.highNum.setNum(Number(this.moreUI.context.data.Value.TokenInfo.Balance) + (this.autoRotation.getThisMoney() * 10));
        this.lowNum.setNum(Number(this.moreUI.context.data.Value.TokenInfo.Balance) - (this.autoRotation.getThisMoney() * 10));
    }

    private initEvent() {
        this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeEvent, this);
        this.moneyHighAddBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.add_begin, this);
        this.moneyHighAddBtn.addEventListener(egret.TouchEvent.TOUCH_END, this.add_end, this);
        this.moneyHighAddBtn.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.add_end, this);
        this.moneyLowAddBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.add_begin, this);
        this.moneyLowAddBtn.addEventListener(egret.TouchEvent.TOUCH_END, this.add_end, this);
        this.moneyLowAddBtn.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.add_end, this);
        this.timeAddBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.add_begin, this);
        this.timeAddBtn.addEventListener(egret.TouchEvent.TOUCH_END, this.add_end, this);
        this.timeAddBtn.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.add_end, this);
        

        this.moneyHighSubBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.begin_sub, this);
        this.moneyHighSubBtn.addEventListener(egret.TouchEvent.TOUCH_END, this.end_sub, this);
        this.moneyHighSubBtn.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.end_sub, this);
        this.moneyLowSubBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.begin_sub, this);
        this.moneyLowSubBtn.addEventListener(egret.TouchEvent.TOUCH_END, this.end_sub, this);
        this.moneyLowSubBtn.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.end_sub, this);
        this.timeSubBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.begin_sub, this);
        this.timeSubBtn.addEventListener(egret.TouchEvent.TOUCH_END, this.end_sub, this);
        this.timeSubBtn.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.end_sub, this);

        this.rotationBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.rotationBtnBeginEvent, this);
        this.rotationBtn.addEventListener(egret.TouchEvent.TOUCH_END, this.rotationBtnEndEvent, this);
    }

    private closeEvent(evt: egret.Event) {
        this.$setVisible(false);
        this.moreUI.context.soundctr.playSound("click_mp3");
    }

    private add_begin(evt: egret.Event) {
        let name = evt.target.name;
        this.tapName = name;
        this.tapNameAction();
        if (this.timer.running) {
            this.timer.stop();
        }
        this.timeoutindex = egret.setTimeout(function () {
            this.timer.start();
        }, this, 1000);
    }

    private add_end(evt: egret.Event) {
        this.timer.stop();
        this.tapName = null;
        if (this.timeoutindex) {
            egret.clearTimeout(this.timeoutindex);
        }
        this.timeoutindex = 0;
    }


    private begin_sub(evt: egret.Event) {
        let name = evt.target.name;
        this.tapName = name;
        this.tapNameAction();
        if (this.timer.running) {
            this.timer.stop();
        }
        this.timeoutindex = egret.setTimeout(function () {
            this.timer.start();
        }, this, 1000);
    }

    private end_sub(evt: egret.Event) {
        this.timer.stop();
        this.tapName = null;
        if (this.timeoutindex) {
            egret.clearTimeout(this.timeoutindex);
        }
        this.timeoutindex = 0;
    }


    private rotationBtnBeginEvent(evt: egret.Event) {
        this.rotationBtn.scaleX = 0.9;
        this.rotationBtn.scaleY = 0.9;
    }

    private rotationBtnEndEvent(evt: egret.Event) {
        this.rotationBtn.scaleX = 1;
        this.rotationBtn.scaleY = 1;
        window.setTimeout(this.moreUI.context.gunDeal.gunSetNumRotate(this.timeNum.getNum(), this.getRotationArr()), 1000);
        this.$setVisible(false);
        this.moreUI.$setVisible(false);
    }



    private getRotationArr() {

        var rotationArr = this.rotationArr;
        rotationArr["high"] = this.highNum.getNum();
        rotationArr["low"] = this.lowNum.getNum();
        return rotationArr;

    }


    private timerFunc(event: egret.Event) {
        if (this.tapName) {
            this.tapNameAction();
        }
    }

    private tapNameAction(): void {
        let letfMoney = Number(this.moreUI.context.data.Value.TokenInfo.Balance);
        switch (this.tapName) {
            case "high_sub":
                {
                    let num = this.highNum.getNum() - 1;
                    if (num > letfMoney)
                        this.highNum.setNum(num);
                    else{
                        this.moneyHighSubBtn.enabled = false;
                    }
                    break;
                }
            case "low_sub":
                {
                    let num = this.lowNum.getNum() - 1;
                    this.moneyLowAddBtn.enabled = true;
                    if (num > 0)
                        this.lowNum.setNum(num);
                    else
                        this.moneyLowSubBtn.enabled = false;
                    break;
                }
            case "time_sub":
                {
                    let num = this.timeNum.getNum() - 1 ;
                    if (num > 0){
                        this.timeNum.setNum(this.timeNum.getNum() - 1);
                        if(num == 1){
                             this.timeSubBtn.enabled = false;
                        }
                    }
                    
                    break;
                }
            case "high_add":
                {
                    this.moneyHighSubBtn.enabled = true;
                    this.highNum.setNum(this.highNum.getNum() + 1);
                    break;
                }
            case "low_add":
                {
                    this.moneyLowSubBtn.enabled = true;
                    let num = this.lowNum.getNum() + 1;
                    if (num < letfMoney)
                        this.lowNum.setNum(this.lowNum.getNum() + 1);
                    else
                        this.moneyLowAddBtn.enabled = false;
                    break;
                }
            case "time_add":
                {
                    this.timeSubBtn.enabled = true;
                    this.timeNum.setNum(this.timeNum.getNum() + 1);
                    break;
                }
        }
        this.moreUI.context.soundctr.playSound("click_mp3");
    }




}